package com.cloud.api;

import com.cloud.api.limiter.HostAddrKeyResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.GatewayFilterSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.route.builder.UriSpec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

@SpringBootApplication
@Configuration
@ComponentScan({"com.cloud.api","com.cloud.base"})
public class CloudApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudApiApplication.class, args);
    }

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
//        .route("host_route", r -> r.path("/admin/admin/**").filters(filter()).uri("lb://cloud-admin"))
//        .route("host_route", r -> r.path("/admin/account/**").filters(filter()).uri("lb://cloud-admin-account"))
//        .route("host_route", r -> r.path("/admin/customer/**").filters(filter()).uri("lb://cloud-admin-customer"))
//        .route("host_route", r -> r.path("/admin/data/**").filters(filter()).uri("lb://cloud-admin-data"))
//        .route("host_route", r -> r.path("/admin/financial/**").filters(filter()).uri("lb://cloud-admin-financial"))
//        .route("host_route", r -> r.path("/admin/operation/**").filters(filter()).uri("lb://cloud-admin-operation"))

//        ////.route("host_route", r -> r.path("/seller/seller/**").filters(filter()).uri("lb://cloud-seller"))
//        .route("host_route", r -> r.path("/seller/center/**").filters(filter()).uri("lb://cloud-seller-center"))
//        ////.route("host_route", r -> r.path("/seller/finance/**").filters(filter()).uri("lb://cloud-seller-finance"))
//        .route("host_route", r -> r.path("/seller/manage/**").filters(filter()).uri("lb://cloud-seller-manage"))
//        ////.route("host_route", r -> r.path("/seller/order/**").filters(filter()).uri("lb://cloud-seller-order"))
//        ////.route("host_route", r -> r.path("/seller/other/**").filters(filter()).uri("lb://cloud-seller-center"))

        ////.route("host_route", r -> r.path("/app/buyer/**").filters(filter()).uri("lb://cloud-buyer"))
        .route("host_route", r -> r.path("/app/game/**").filters(filter()).uri("lb://cloud-buyer-game"))
        .route("host_route", r -> r.path("/app/personal/**").filters(filter()).uri("lb://cloud-buyer-personal"))

        .build();
    }

    private Function<GatewayFilterSpec, UriSpec> filter() {
        return f -> f
        .hystrix(c -> c.setName("slowcmd")).stripPrefix(1)
        .requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter)
        .setKeyResolver(hostAddrKeyResolver));
    }


    @Autowired
    private RedisRateLimiter redisRateLimiter;
    @Autowired
    private HostAddrKeyResolver hostAddrKeyResolver;
}
